/**
 * 
 */
package com.manage.service;

import java.util.List;

import org.apache.log4j.Logger;

import com.manage.model.OrderConfig;
import com.manage.config.Config;
import com.service.AbstractService;
import com.util.ConfigUtil;
import com.utils.dbcp.ConnectionManager;
import com.utils.dbcp.ModelGenerator;

public class OrderConfigServiceImpl extends AbstractService implements OrderConfigService
{
	private Logger logger;
	
	public OrderConfigServiceImpl()
	{
		logger = Logger.getLogger(this.getClass());
		this.dbcp = ConfigUtil.get(Config.SECTION, Config.KEY_DBCP_NAME);
	}
		

	@Override
	public List<OrderConfig> getOrderConfigs()
	{
		String sql = "select * from orderconfig order by id";
		logger.debug("sql: " + sql);
		ModelGenerator<OrderConfig> dbcpmg = new ModelGenerator<OrderConfig>(OrderConfig.class);
		return dbcpmg.generateList(dbcp, sql);
	}


	@Override
	public boolean saveOrderConfig(List<OrderConfig> list, int type,int cpid) {
		
		String [] sql = new String[list.size()+1];
		if(type==0)
		{
			sql[0]="delete from orderconfig where apknumber=''";
		}else if(type==1)
		{
			sql[0]="delete from orderconfig where apknumber!='' and cpid='"+cpid+"'";
		}
		for(int i = 0;i<list.size();i++)
		{
			OrderConfig model = list.get(i);
			model.setId(getTableId("orderconfig"));
			sql[i+1] = "insert into orderconfig(id,cpid,apknumber,percentage) values ('"+model.getId()+"','"+model.getCpid()+"','"+model.getApknumber()+"','"+model.getPercentage()+"')";
		}
		return ConnectionManager.getInstance().executeBatch(dbcp, sql);
	}
	
}
